package com.icss.common.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.icss.common.util.ConstatFinalUtil;
import com.icss.common.util.VerifyCodeUtils;
import com.icss.common.vo.ApiResponse;
import com.icss.goods.service.IGoodsDbService;
import com.icss.goods.vo.ReGoods;

/**
 * 公共的方法
 * @author 刘鑫
 */
@Controller
@RequestMapping("/common/")
public class CommonController extends BaseController
{
	@Resource
	private IGoodsDbService goodsDbService;
	
	/**
	 * 生成验证码
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/randImg")
	public void randImg(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		response.setHeader("Pragma", "No-cache");  
		response.setHeader("Cache-Control", "no-cache");  
		response.setDateHeader("Expires", 0);  
		response.setContentType("image/jpeg");  
         
       //生成随机字串  
       String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
       //存入会话session  
       HttpSession session = request.getSession(true);  
       session.setAttribute("randSess", verifyCode);  
       //生成图片  
       int w = 200, h = 80;  
       VerifyCodeUtils.outputImage(w, h, response.getOutputStream(), verifyCode); 
	}
	
	/**
	 * 生成验证码
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/info")
	public String info(HttpServletRequest request,HttpServletResponse response) throws IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("==info==");
		return "/info" ; 
	}
	
	/**
	 * 专门用来处理上传图片
	 * @return
	 * @throws ServletException 
	 */
	@RequestMapping("/uploadFile")
	public String uploadFile(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		/* 准备路径 */
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String fileType = "未知类型" ; 
		String operType = request.getParameter("operType");
		if("users".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			fileType = "用户头像";
		}
		if (updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		request.setAttribute("fileType", fileType);
		request.setAttribute("response", apiResponse.toJSON());
		return "uploadFile";
	}
	
	/**
	 * 专门用来处理上传图片
	 * @return
	 */
	@RequestMapping(value = "uploadFileSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String uploadFileSubmit(HttpServletRequest request, MultipartFile file) throws IOException
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		
		String fileName = file.getOriginalFilename() ; 
		InputStream is = (InputStream) file.getInputStream() ;
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("fileName", fileName);
		paramsMap.put("is", is);
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 相关id  */
		String relaId = request.getParameter("relaId");
		/* 类型 */
		String operType = request.getParameter("operType");
		if("goods".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			condMap.clear();
			condMap.put("id", relaId);
			ApiResponse<ReGoods> apiResponseUsers = this.goodsDbService.findOneGoodsService(condMap);
			ReGoods goods = apiResponseUsers.getDataOneJava() ; 
			
			/* 上传图片 */
			apiResponse = this.goodsDbService.updateOneGoodsService(goods, paramsMap);
			
		}
		ConstatFinalUtil.SYS_LOGGER.info("上传照片;relaId:{},结果:{}",relaId,apiResponse.toJSON().toJSONString());
		return apiResponse.toJSON().toJSONString() ; 
	}
	
	/**
	 * 专门用来处理上传图片
	 * @return
	 * @throws ServletException 
	 */
	@RequestMapping("/uploadFileBack")
	public String uploadFileBack(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
	{
		/* 准备路径 */
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		String fileType = "未知类型" ; 
		String operType = request.getParameter("operType");
		if("goods".equalsIgnoreCase(operType))
		{
			fileType = "商品图片";
		}
		if (updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		request.setAttribute("fileType", fileType);
		request.setAttribute("response", apiResponse.toJSON());
		return "/uploadFileBack" ; 
	}
	
	/**
	 * 专门用来处理上传图片
	 * @return
	 */
	@RequestMapping(value = "uploadFileBackSubmit",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String uploadFileBackSubmit(HttpServletRequest request, MultipartFile file) throws IOException
	{
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		
		String fileName = file.getOriginalFilename() ; 
		InputStream is = (InputStream) file.getInputStream() ; 
		
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("fileName", fileName);
		paramsMap.put("is", is);
		
		/* 搜索条件 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 相关id  */
		String relaId = request.getParameter("relaId");
		/* 类型 */
		String operType = request.getParameter("operType");
		if("goods".equalsIgnoreCase(operType))
		{
			/* 用户上传头像 */
			condMap.clear();
			condMap.put("id", relaId);
			ApiResponse<ReGoods> apiResponseUsers = this.goodsDbService.findOneGoodsService(condMap);
			ReGoods goods = apiResponseUsers.getDataOneJava() ; 
			
			/* 上传图片 */
			apiResponse = this.goodsDbService.updateOneGoodsService(goods, paramsMap);
			
		}
		ConstatFinalUtil.SYS_LOGGER.info("上传照片;relaId:{},结果:{}",relaId,apiResponse.toJSON().toJSONString());
		/* 返回结果 */
		return apiResponse.toJSON().toJSONString() ; 
	}
}
